package com.rzj.demo.demos.mapper;

import com.rzj.demo.demos.model.UserInfo;
import lombok.Data;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    // 拿到全部的信息
    @Select("select * from user_info")
    List<UserInfo> selectAll();

    // 拿到一条信息
    // 对字段进行重命名
    @Select("select id as Id, username as Username, password as Password, age, gender, phone, delete_flag, create_time, update_time" +
            " from user_info where id = #{id11123}")
    UserInfo selectOne(Integer id);

    @Select("select * from user_info where id = #{userId}")
    UserInfo selectOne2(@Param("userId") Integer id);

    // 增,返回的是受影响的行数
    // 如果注解中字符串太长,使用 + 号进行拼接
    // 返回自增id
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username,password,age,gender,phone)" +
            "values(#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);// 实现

    // 对insert的参数进行重命名
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username,password,age,gender,phone)" +
            "values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);// 实现

    // 删
    @Delete("delete from user_info where id=#{id}")
    Integer delete(Integer id);

    // 改
    @Update("update user_info set age =#{age} where id = #{id}")
    Integer update(UserInfo userInfo);


    // 排序
    @Select("select * from user_info order by id ${sort}")
    List<UserInfo> selectUserBySort(String sort);
}
